<?php

namespace app\common\model;

use Think\Db;
use think\Model;


class Depot extends Model
{

    

    

    // 表名
    protected $name = 'depot';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'is_close_text',
        'allowance',
        'kc',
        'use_num'
    ];
    

    
    public function getIsCloseList()
    {
        return ['0' => __('Is_close 0'), '1' => __('Is_close 1')];
    }
    //已使用数量
    public function getUseNumAttr($value, $data)
    {
        return Db::name('cdkey')
            ->where('depot_id',$data['id'])
            ->where('is_start','1')
            ->where('is_use','1')
            ->count('id');
    }
    //根据兑换码获取库存
    public function getKcAttr($value, $data)
    {
        return Db::name('cdkey')
            ->where('depot_id',$data['id'])
            ->count('id');
    }
    //获取剩余库存兑换码
    public function getAllowanceAttr($value, $data)
    {
        $kc =  Db::name('cdkey')
            ->where('depot_id',$data['id'])
            ->count('id');
        $num = Db::name('cdkey')
            ->where('depot_id',$data['id'])
            ->where('is_start','1')
            ->where('is_use','1')
            ->count('id');
        return $kc-$num;
    }

    public function getIsCloseTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['is_close']) ? $data['is_close'] : '');
        $list = $this->getIsCloseList();
        return isset($list[$value]) ? $list[$value] : '';
    }




}
